<?php
class ControllerModuleArticleCatalog extends Controller {
    public function index() {
		static $module = 0;
        $this->language->load('module/article_list');	
		$this->load->model('catalog/category');	
        $this->load->model('news/category');
		$this->load->model('news/install');
		$this->model_news_install->checkDB();
        $this->load->model('news/article');	
		$this->data['width'] = $this->config->get('config_related_catalog_image_width');
		$this->data['text_related'] = $this->language->get('text_related');	
		$this->data['text_date_added'] = $this->language->get('text_date_added');	
        $this->data['text_refine'] = $this->language->get('text_refine');
        $this->data['text_empty'] = $this->language->get('text_empty');
        $this->data['text_author'] = $this->language->get('text_author');
        $this->data['text_tax'] = $this->language->get('text_tax');
        $this->data['button_cart'] = $this->language->get('button_cart');
			
		if (isset($this->request->get['path'])) {
			$path = '';
		
			$parts = explode('_', (string)$this->request->get['path']);
		
			foreach ($parts as $path_id) {
				if (!$path) {
					$path = (int)$path_id;
				} else {
					$path .= '_' . (int)$path_id;
				}
									
				$category_info = $this->model_catalog_category->getCategory($path_id);
				
				if ($category_info) {
	       			$this->data['breadcrumbs'][] = array(
   	    				'text'      => $category_info['name'],
						'href'      => $this->url->link('product/category', 'path=' . $path),
        				'separator' => $this->language->get('text_separator')
        			);
				}
			}		
		
			$category_id = (int)array_pop($parts);
		} else {
			$category_id = 0;
		}
		
		$catalog_category_info = $this->model_catalog_category->getCategory($category_id);	
		if($catalog_category_info&&$catalog_category_info['related_id']){
		if (!empty($catalog_category_info)) {
			$article_category_id = $catalog_category_info['related_id'];
		} else {
			$article_category_id = 0;
		}
		
		if($this->config->get('config_related_description_limit')){
			$description_limit=(int)$this->config->get('config_related_description_limit');
		}else{
			$description_limit=255;
		}	
			
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}	
							
		if ($this->config->get('config_related_article_limit')>0) {
			$limit = $this->config->get('config_related_article_limit');
		} else {
			$limit = 1;
		}
				
		if ($this->config->get('config_related_children_limit')>0) {
			$children_limit = $this->config->get('config_related_children_limit');
		} else {
			$children_limit = 0;
		}
		
        $category_info = $this->model_news_category->getCategory($article_category_id);
		
        $this->data['href'] = 'index.php?route=module/article_catalog/getlist&cpath=' . $article_category_id .'&limit=' .$limit;
        $this->data['heading_title'] = $category_info['name'];
		/*sub category*/ 		
		
		$this->data['categories'] = array();

		$categories = $this->getCategories($article_category_id,$children_limit);

		foreach ($categories as $category) {
			$total = $this->model_news_article->getTotalArticles(array('filter_article_category_id'  => $category['article_category_id']));
			$this->data['categories'][] = array(
				'article_category_id' => $category['article_category_id'],
				'name'        => $category['name'],
				'total'        => $total,
				'href'        => 'index.php?route=module/article_catalog/getlist&cpath='. $category['article_category_id'].'&limit=' .$limit
			);	
		}		
		/*children category*/ 			
        $this->data['article_ajax_load'] = $this->getArticle($article_category_id,$page,$limit,$description_limit);
			
        $page = 1;		
		$data = array(
				'filter_article_category_id' => $article_category_id, 
				'sort'               => 'p.date_added',
				'order'              => 'DESC',
				'start'              => ($page - 1) * $limit,
				'limit'              => $limit
			);
			$article_total = $this->model_news_article->getTotalArticles($data); 		
			$pagination = new Pagination();
			$pagination->total = $article_total;
			$pagination->num_links = 3;
			$pagination->page =$page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = 'index.php?route=module/article_catalog/getlist&cpath='. $article_category_id .'&page={page}'.'&limit=' .$limit;
			$this->data['pagination'] = $pagination->render();
		
		$this->data['module'] = $module++; 
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/blog/article_catalog/article_ajax_load.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/blog/article_catalog/article_ajax_load.tpl';
        } else {
            $this->template = 'default/template/module/blog/article_catalog/article_ajax_load.tpl';
        }

        $this->response->setOutput($this->render());
		}
    }
	public function getCategories($parent_id = 0, $limit) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_category c 
		LEFT JOIN " . DB_PREFIX . "news_category_description cd ON (c.article_category_id = cd.article_category_id) 
		LEFT JOIN " . DB_PREFIX . "news_category_to_store c2s ON (c.article_category_id = c2s.article_category_id) 
		WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' 
		AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name) LIMIT " . (int)$limit . "");		
		return $query->rows;
	}
    protected function getArticle($article_category_id,$page,$limit,$description_limit) {
        $this->load->model('news/category');
		$this->data['text_more'] = $this->language->get('text_more');
		$this->data['height'] = $this->config->get('config_related_catalog_image_height');
        $this->load->model('news/article');
		$this->load->model('news/utf8');
        $this->load->model('tool/image');
        $this->data['articles'] = array();
        $data = array(
            'filter_article_category_id' => $article_category_id,
            'sort' => 'p.date_added',
            'order' => 'DESC',
            'start' => ($page - 1) * $limit,
            'limit' => $limit
        );
        $article_total = $this->model_news_article->getTotalArticles($data);
        $results = $this->model_news_article->getArticles($data);
		
		if($this->config->get('config_related_catalog_image_width')){
			$image_width=(int)$this->config->get('config_related_catalog_image_width');
		}else{
			$image_width=150;
		}	
		if($this->config->get('config_related_catalog_image_height')){
			$image_height=(int)$this->config->get('config_related_catalog_image_height');
		}else{
			$image_height=112;
		}
        foreach ($results as $result) {
		$related_articles = array();
			
			$related = $this->model_news_article->getArticleRelatedLimit($result['article_id'],3);	
			foreach ($related as $relate) {							
				$related_articles[] = array(
					'article_id' => $relate['article_id'],
					'name'    	 => $relate['name'],
					'href'    	 => $this->url->link('news/article', 'article_id=' . $relate['article_id']),
				);
			}	
			
            if ($result['image']) {
                $image = $this->model_tool_image->resize($result['image'], $image_width, $image_height);
            } else {
                $image = false;
            }
            if ($this->config->get('config_comment_status')) {
                $rating = (int) $result['rating'];
            } else {
                $rating = false;
            }
            $this->data['articles'][] = array(
                'article_id' => $result['article_id'],
                'date_added' => date($this->config->get('config_article_date_format'), strtotime($result['date_added'])),
                'related' => $related_articles,
                'thumb' => $image,
                'name' => $result['name'],
                'author' => $result['author'],
				'author_href'    	 => $this->url->link('news/author/info', 'author_id=' . $result['author_id']),
                'description' => $this->model_news_utf8->utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $description_limit) . '..',
                'rating' => $result['rating'],
				'comments'     => sprintf($this->language->get('text_comments'), (int)$result['comments']),
				'viewed'     => sprintf($this->language->get('text_viewed'), (int)$result['viewed']),
                'href' => $this->url->link('news/article', 'cpath=' . $article_category_id . '&article_id=' . $result['article_id'])
            );
        }
    }
	public function getlist(){	
        $this->language->load('module/article_list');
		$this->load->model('news/category');		
		$this->load->model('news/article');
		$this->data['width'] = $this->config->get('config_related_catalog_image_width');
		$this->data['text_empty'] = $this->language->get('text_empty');		
		$this->data['text_related'] = $this->language->get('text_related');	
		$this->data['text_date_added'] = $this->language->get('text_date_added');	
		$this->data['text_author'] = $this->language->get('text_author');	
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}	
							
		if (isset($this->request->get['limit'])) {
			$limit = $this->request->get['limit'];
		} else {
			$limit = 3;
		}
							
		if (isset($this->request->get['cpath'])) {
			$article_category_id = $this->request->get['cpath'];
		} else {
			$article_category_id = 0;
		}			
		$category_info = $this->model_news_category->getCategory($article_category_id);				
			
			$data = array(
				'filter_article_category_id' => $article_category_id, 
				'sort'               => 'p.date_added',
				'order'              => 'DESC',
				'start'              => ($page - 1) * $limit,
				'limit'              => $limit
			);
					
			$article_total = $this->model_news_article->getTotalArticles($data); 		
		
		if($this->config->get('config_related_description_limit')){
			$description_limit=(int)$this->config->get('config_related_description_limit');
		}else{
			$description_limit=255;
		}	
        $this->data['article_ajax_load'] = $this->getArticle($article_category_id,$page,$limit,$description_limit);
					
			$pagination = new Pagination();
			$pagination->total = $article_total;
			$pagination->num_links = 3;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = 'index.php?route=module/article_catalog/getlist&cpath='.$article_category_id. '&page={page}'.'&limit=' .$limit;
		
			$this->data['pagination'] = $pagination->render();
		
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/blog/article_catalog/article_ajax_list.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/blog/article_catalog/article_ajax_list.tpl';
        } else {
            $this->template = 'default/template/module/blog/article_catalog/article_ajax_list.tpl';
        }
       	$this->response->setOutput($this->render());
	}

}

?>